package com.jamcodingnm.apps.abqevents.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.jamcodingnm.apps.abqevents.R;


/**
 * @author jeremy,mgonzales
 * 
 */
public class ItemDetailActivity extends SingleFragmentActivity {
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			this.setTheme(com.actionbarsherlock.R.style.Theme_Sherlock);
		}
		
		super.onCreate(savedInstanceState);
		
		if (savedInstanceState == null) {
			// Set fragment arguments here.

		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.item_description_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_share:
				shareThisEvent(item.getActionView());
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	/**
	 * Used to pop up display chooser window to let users share the event.
	 * 
	 * @param view
	 *            <code>Object unused</code>
	 */
	public void shareThisEvent(View view) {
		// Create a new intent
		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);

		// Set the intent type
		shareIntent.setType("text/plain");

		// Get the bundle that contains the data from the Item Activity
		Bundle bundle = getIntent().getExtras();
		bundle = bundle.getBundle("android.intent.extra.INTENT");

		String title = "";
		String shareMessage = "";
		Object textViewFoundObject = null;

		// Finds the Object by ID and checks for null values
		TextView textView = (TextView) findViewById(R.id.rss_item_title);
		textViewFoundObject = textView.getText();

		if (textViewFoundObject != null) {
			shareMessage = (String) textView.getText();
			title = shareMessage;
		}

		textView = (TextView) findViewById(R.id.rss_item_description);
		textViewFoundObject = textView.getText();

		if (textViewFoundObject != null)
			shareMessage = shareMessage + "\n" + (String) textView.getText();

		textView = (TextView) findViewById(R.id.rss_item_details);
		textViewFoundObject = textView.getText();

		if (textViewFoundObject != null)
			shareMessage = shareMessage + "\n" + (String) textView.getText();

		textView = (TextView) findViewById(R.id.rss_item_link);
		textViewFoundObject = textView.getText();

		if (textViewFoundObject != null)
			shareMessage = shareMessage + "\n" + (SpannableString) textView.getText();

		// Adding subject and text
		shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
		shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareMessage);

		// Chooser text
		startActivity(Intent.createChooser(shareIntent, "Share via"));
	}
	
	@Override
	protected Fragment onCreateFragment() {
		return new ItemDetailFragment();
	}
}